#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/main.h"

struct fileDescriptor * createDataFile(char name1[],char name[],char location[])
{
	FILE *fp;
	struct header h1;
	struct Block b1;
	struct freeList fl1;
	struct fileDescriptor fd1,*fd2;
	int i,j,n,size1,size2,blockno,freelistno,freefileDescriptor;
	freefileDescriptor=0;
	char datas[1024];
	printf("\nEnter data for file : ");
	scanf("%[^\t]s",datas);
		
	printf("%s\n%s\n%s\n",name1,name,location);	
	printf("in createdatafile\n");
	//fgets(datas, sizeof(datas), stdin);
	fp=fopen(name,"rb+");
	fread(&h1,sizeof(struct header),1,fp);
	n=h1.maxfd;
	fseek(fp,sizeof(struct header),SEEK_SET);
	printf("Number:%d",n);
//---------------------------------------------------------------------------------------------------------------------
//searching for free list 	
	for(i=0;i<n;i++)
	{
		fread(&fl1,sizeof(struct freeList),1,fp);
		if(fl1.freel == '0')
		{
			fl1.freel = '1';
			size1=sizeof(struct header)+i*sizeof(struct freeList);
			fseek(fp,size1,SEEK_SET);
			fwrite(&fl1,sizeof(struct freeList),1, fp );
			freelistno=i;
			//printf("freelist:%c\n",fl1.freel);
			//printf("Number:%d\n",i);
			break;
		}
	}
//------------------------------------------------------------------------------------------------------------------------
//searching for free file descriptor
	size1=sizeof(struct header)+n*sizeof(struct freeList);
	fseek(fp,size1,SEEK_SET);
	strcpy(fd1.fileName,name1);
	fd1.blockNumber=freelistno;
	for(i=0;i<n;i++)
	{
		fread(&fd1,sizeof(struct fileDescriptor),1,fp);
		if(fd1.isfull == '0')
		{
			freefileDescriptor=1;
			blockno=i;
			fd1.isfull='1';
			fd1.blockNumber=freelistno;
			strcpy(fd1.fileType,"text");;
			strcpy(fd1.fileName,name1);
			strcpy(fd1.locationPath,location);
			//size2=sizeof(struct header)+n*sizeof(struct freeList)+i*sizeof(struct fileDescriptor);
			//fseek(fp,size2,SEEK_SET);
			//fwrite(&fd1,sizeof(struct fileDescriptor),1, fp );
			//printf("fd:%c\n",fd1.isfull);
			//printf("Number:%d\n",i);
			break;
		}
		
	}
		if(freefileDescriptor==0)
		{
			return NULL;
		}
//----------------------------------------------------------------------------------------------------------------------
/*	fseek(fp,sizeof(struct header),SEEK_SET);
	for(i=0;i<n;i++)
	{
		fread(&fl1,sizeof(struct freeList),1,fp);
		printf("%c\t",fl1.freel);
	}

	//fseek(fp,sizeof(struct header),SEEK_SET);
	for(i=0;i<n;i++)
	{
		fread(&fd1,sizeof(struct fileDescriptor),1,fp);
		printf("%c\t",fd1.isfull);
	}*/
//----------------------------------------------------------------------------------------------------------------------
//writing data to blocks
	size1=sizeof(struct header)+n*sizeof(struct freeList);
		size2=(size1+(n*(sizeof(struct fileDescriptor)))+(freelistno*(sizeof(struct Block))));
	fseek(fp,size2,SEEK_SET);
	strcpy(b1.text,datas);
	b1.isfull='1';
	fwrite(&b1,sizeof(struct Block),1, fp );
	printf("\nFile written to disk\n");
	

//------------------------------------------------------------------------------------------------------------------------
	fseek(fp,sizeof(struct header),SEEK_SET);
	for(i=0;i<n;i++)
	{
		printf("flist->");
		fread(&fl1,sizeof(struct freeList),1,fp);
		printf("%c\t",fl1.freel);
		
	}
	printf("\n");
	/*for(i=0;i<n;i++)
	{
		fread(&fd1,sizeof(struct fileDescriptor),1,fp);
		printf("%c\t",fd1.isfull);
	}*/
	size1=sizeof(struct header)+n*sizeof(struct freeList);
	size2=(size1+(n*(sizeof(struct fileDescriptor)))+(freelistno*(sizeof(struct Block))));
	fseek(fp,size2,SEEK_SET);
	printf("\n");
	for(i=0;i<n;i++)
	{
		fread(&b1,sizeof(struct Block),1,fp);
		//printf("%c\t",b1.isfull);
		if(b1.isfull=='1')
		printf("\nFile contents : %s \n",b1.text);
	}
	fclose(fp);
	fd2=&fd1;
	return fd2;
}
